//
//  DLOpinionReportVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2019/1/18.
//  Copyright © 2019 delongzhitou. All rights reserved.
//

#import "DLOpinionReportVC.h"

@interface DLOpinionReportVC ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *typeBgView;

@property (nonatomic, strong) UITextView *introduceTextView;

@property (nonatomic, strong) UILabel *characterCountLabel;

@property (nonatomic, strong) UITextField *contactMethodTF;

@property (nonatomic, assign) NSInteger opinionType;

@end

@implementation DLOpinionReportVC

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.opinionType = 1;
    [self setUpNav];
    [self layoutSubControls];
    // Do any additional setup after loading the view.
}
- (void) layoutSubControls {
    
    self.view.backgroundColor = kColorRGB(240, 240, 240);
    
    UIView *typeBgView = [UIView new];
    typeBgView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    typeBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:typeBgView];
    self.typeBgView = typeBgView;
    
    UILabel *typeLabel = [UILabel new];
    typeLabel.frame = CGRectMake(15, 0, 45, typeBgView.height);
    typeLabel.text = @"类型：";
    typeLabel.textColor = kDLNormalGrayColor;
    typeLabel.textAlignment = NSTextAlignmentLeft;
    typeLabel.font = kSystemFont(14);
    [self.view addSubview:typeLabel];
    
    NSArray *titleArr = @[@"功能异常", @"产品建议", @"其他"];
    CGFloat buttonWidth = kAutoWidth(100);
    CGFloat buttonHeight = typeBgView.height;
    for (NSInteger i = 0; i < titleArr.count; i++) {
        UIButton *typeBtn = [UIButton buttonWithType:0];
        typeBtn.frame = CGRectMake(typeLabel.right + buttonWidth *i, 0, buttonWidth, buttonHeight);
        [typeBtn setImage:[UIImage imageNamed:@"opinion_btnNormal"] forState:UIControlStateNormal];
        [typeBtn setImage:[UIImage imageNamed:@"opinion_btnSelected"] forState:UIControlStateSelected];
        [typeBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        typeBtn.titleLabel.font = kSystemFont(14);
        [typeBtn setTitleColor:kDLNormalGrayColor forState:UIControlStateNormal];
        typeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [typeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        typeBtn.tag = 100 + i;
        [typeBgView addSubview:typeBtn];
        [typeBtn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            typeBtn.selected = YES;
        }
    }
    
    
    UIView *contentBgView = [UIView new];
    contentBgView.frame = CGRectMake(typeBgView.left, typeBgView.bottom + 15, typeBgView.width, kAutoWidth(369));
    contentBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentBgView];
    
    UILabel *contentAlertLabel = [UILabel new];
    contentAlertLabel.frame = CGRectMake(15, 0, contentBgView.width - 30, typeBgView.height);
    contentAlertLabel.text = @"内容描述：";
    contentAlertLabel.textColor = kDLNormalGrayColor;
    contentAlertLabel.textAlignment = NSTextAlignmentLeft;
    contentAlertLabel.font = kSystemFont(14);
    [contentBgView addSubview:contentAlertLabel];
    
    UITextView *introduceTextView = [UITextView new];
    introduceTextView.frame = CGRectMake(contentAlertLabel.left, contentAlertLabel.bottom, contentAlertLabel.width, kAutoWidth(194));
    introduceTextView.delegate = self;
    introduceTextView.tag = 95533;
    
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"请您输入宝贵的意见，小语会为您不断改进(最少十个字)。";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = kColorRGB(182, 182, 182);
    [placeHolderLabel sizeToFit];
    [introduceTextView addSubview:placeHolderLabel];//这句很重要不要忘了
    
    introduceTextView.backgroundColor = kColorRGB(240, 240, 240);
    introduceTextView.font = kSystemFont(14);
    placeHolderLabel.font = kSystemFont(14);
    [introduceTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    
    
//    introduceTextView.layer.borderWidth = 1;
//    introduceTextView.layer.borderColor = kColorRGB(244, 244, 244).CGColor;
    [contentBgView addSubview:introduceTextView];
    self.introduceTextView = introduceTextView;
    
    
    UILabel *characterCountLabel = [UILabel new];
    characterCountLabel.frame = CGRectMake(introduceTextView.left, introduceTextView.bottom + 5, contentBgView.width - 30, 20);
    characterCountLabel.text = @"0/512";
    characterCountLabel.font = kSystemFont(14);
    characterCountLabel.textColor = kDLNormalGrayColor;
    characterCountLabel.textAlignment = NSTextAlignmentRight;
    [contentBgView addSubview:characterCountLabel];
    self.characterCountLabel = characterCountLabel;
    
    UILabel *contactMethodLabel = [UILabel new];
    contactMethodLabel.frame = CGRectMake(contentAlertLabel.left, characterCountLabel.bottom, contentAlertLabel.width, contentAlertLabel.height);
    contactMethodLabel.text = @"联系方式：";
    contactMethodLabel.textColor = kDLNormalGrayColor;
    contactMethodLabel.textAlignment = NSTextAlignmentLeft;
    contactMethodLabel.font = kSystemFont(14);
    [contentBgView addSubview:contactMethodLabel];
    
    
    UIView *contactMethodTFBgView = [UIView new];
    contactMethodTFBgView.frame = CGRectMake(contactMethodLabel.left, contactMethodLabel.bottom, contactMethodLabel.width, contactMethodLabel.height);
    contactMethodTFBgView.backgroundColor = kColorRGB(240, 240, 240);
    [contentBgView addSubview:contactMethodTFBgView];
    
    
    UITextField *contactMethodTF = [UITextField new];
    contactMethodTF.frame = CGRectMake(6, 0, contactMethodTFBgView.width - 6, contactMethodTFBgView.height);
//    contactMethodTF.backgroundColor = kColorRGB(240, 240, 240);
    contactMethodTF.borderStyle = UITextBorderStyleNone;
    contactMethodTF.keyboardType = UIKeyboardTypeDefault;
    contactMethodTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    contactMethodTF.font = kSystemFont(14);
    contactMethodTF.placeholder = @"请输入您的联系方式";
    [contactMethodTFBgView addSubview:contactMethodTF];
    self.contactMethodTF = contactMethodTF;
    contactMethodTF.text = kuserManager.userInfoModel.mobile?:@"";
    
    
    UIButton *submitButton = [UIButton buttonWithType:0];
    submitButton.frame = CGRectMake((kScreenWidth - kAutoWidth(313)) / 2, contentBgView.bottom + 25, kAutoWidth(313), 40);
    [submitButton setBackgroundColor:kDLBtnNormalBgColor];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    submitButton.titleLabel.font = kSystemFont(17);
    [self.view addSubview:submitButton];
    submitButton.layer.cornerRadius = 20;
    submitButton.clipsToBounds = YES;
    [submitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void) submitButtonClick {
    
    if (self.introduceTextView.text.length < 10) {
        kShowRequestMsg(@"反馈内容最少十个字哦");
        return;
    }
    
    NSString *postUrl = HTTPOpinionReport;
    NSDictionary *postDic = @{
                              @"type" : @(self.opinionType),
                              @"content" : self.introduceTextView.text,
                              @"mobile" : self.contactMethodTF.text?:@""
                              };
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
    }];
}

- (void) textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length > 512) {
        NSString *str = textView.text;
        textView.text = [str substringToIndex:512];
    }
    
    self.characterCountLabel.text = [NSString stringWithFormat:@"%ld/512", textView.text.length];
}
- (void) typeBtnClick:(UIButton *)btn {
    
    for (UIButton *typeButton in self.typeBgView.subviews) {
        typeButton.selected = NO;
    }
    btn.selected = YES;
    
    self.opinionType = btn.tag - 99;
}

- (void) setUpNav {
    
    self.titleView.title = @"意见反馈";
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
